<?php

namespace App\Http\Middleware;

use Closure;

class PermitMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        //得到当前路径
        $path = $this->middlePath($request->path());
        // 判断用户是否拥有操作权限
        $permits = permitsWithPath($request->user()->permits());    //获取
        //检测
        foreach($permits as $permit){
            if($path === $permit->path){
                return $next($request);
            }
        }
        return ['code' => 500, 'msg' => '你没有权限', 'data' => []];
//        return $next($request);
    }


    public function middlePath($path)
    {
        $path = explode('/', $path);
        array_shift($path);
        if(is_numeric($path[count($path)-1])){
            array_pop($path);
        }
        return join('/', $path);
    }
}
